package service

import (
	"context"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/google/uuid"
	"github.com/share309/gojwt"
	"time"
	__ "user_srv/basic/proto"
	"user_srv/handler/model"
	"user_srv/pkg"
)

type Server struct {
	__.UnimplementedUserServer
}

// 注册
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	var user model.User
	user.Mobile = in.Mobile
	user.Password = pkg.Md5(in.Password)
	err := user.GetUserByMobile()
	if err != nil {
		return nil, fmt.Errorf("获取用户信息失败")
	}
	if user.Id > 0 {
		return nil, fmt.Errorf("该账号已注册")
	}
	err = user.CreateUser()
	if err != nil {
		return nil, fmt.Errorf("创建失败")
	}
	return &__.RegisterResp{
		UserId: int64(user.Id),
	}, nil
}

// 登录
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user model.User
	user.Mobile = in.Mobile
	user.Password = pkg.Md5(in.Password)
	err := user.GetUserByMobile()
	if err != nil {
		return nil, fmt.Errorf("获取用户信息失败")
	}
	if user.Id == 0 {
		return nil, fmt.Errorf("该账号未注册")
	}
	token, _ := gojwt.NewJWT("ywy").CreateToken(gojwt.CustomClaims{
		ID: user.Id,
		StandardClaims: jwt.StandardClaims{

			ExpiresAt: time.Now().Unix() * 3600 * 3,
		},
	})

	return &__.LoginResp{
		Token: token,
	}, nil
}

// 商品列表
func (s *Server) GoodsList(_ context.Context, in *__.GetGoodsListReq) (*__.GetGoodsListResp, error) {
	var goods model.Goods
	list, err := goods.GetGoodsList()
	if err != nil {
		return nil, fmt.Errorf("商品列表展示失败")
	}

	var GoodsList []*__.GoodsList
	for _, g := range list {
		GoodsList = append(GoodsList, &__.GoodsList{
			Title:      g.Title,
			Describe:   g.Describe,
			GoodsUrl:   g.GoodsUrl,
			GoodsPrice: float32(g.GoodsPrice),
			GoodsStock: int64(g.GoodsStock),
		})
	}
	return &__.GetGoodsListResp{
		List: GoodsList,
	}, nil
}

// 添加收藏
func (s *Server) AddCollect(_ context.Context, in *__.AddCollectReq) (*__.AddCollectResp, error) {
	var collect model.Collect
	collect.UserId = uint(in.UserId)
	collect.GoodsId = uint(in.GoodsId)
	err := collect.AddCollect()
	if err != nil {
		return nil, fmt.Errorf("收藏失败")
	}
	return &__.AddCollectResp{
		CollectId: int64(collect.Id),
	}, nil
}

// 删除收藏
func (s *Server) DeleteCollect(_ context.Context, in *__.DeleteCollectReq) (*__.DeleteCollectResp, error) {
	var collect model.Collect
	collect.Id = uint(in.CollectId)
	err := collect.DeleteCollect()
	if err != nil {
		return nil, fmt.Errorf("删除失败")
	}
	return &__.DeleteCollectResp{
		CollectId: int64(collect.Id),
	}, nil
}

// 创建订单
func (s *Server) CreateOrder(_ context.Context, in *__.CreateOrderReq) (*__.CreateOrderResp, error) {
	var goods model.Goods
	err := goods.GetGoodsById(int(in.GoodsId))
	if err != nil {
		return nil, fmt.Errorf("查询商品信息失败")
	}
	if goods.Id == 0 {
		return nil, fmt.Errorf("该商品不存在创建订单失败")
	}
	var user model.User
	err = user.GetUserById(int(in.UserId))
	if err != nil {
		return nil, fmt.Errorf("查询用户信息失败")
	}
	if user.Id == 0 {
		return nil, fmt.Errorf("该用户不存在创建订单失败")
	}
	order := model.Order{
		OrderSn:    time.DateTime + uuid.NewString(),
		UserId:     uint(in.UserId),
		GoodsId:    uint(in.GoodsId),
		OrderNum:   uint(in.OrderNum),
		OrderTotal: goods.GoodsPrice * float64(in.OrderNum),
	}
	err = order.CreateOrder()
	if err != nil {
		return nil, fmt.Errorf("创建订单失败")
	}
	return &__.CreateOrderResp{
		OrderId: int64(order.Id),
		PayUrl:  pkg.ALiPay(order.OrderSn, fmt.Sprintf("%.2f", order.OrderTotal)),
	}, nil
}
